package com.springboot.demo.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.springboot.demo.common.customValidator.ListNotHasNull;
import com.springboot.demo.entity.*;
import com.springboot.demo.entity.validate.role.RoleDescriptionGroup;
import com.springboot.demo.entity.validate.role.RoleNameGroup;
import com.springboot.demo.entity.validate.user.IdGroup;
import com.springboot.demo.service.RoleService;
import com.springboot.demo.service.UserService;
import com.springboot.demo.utils.ResponseUtils;
import io.swagger.annotations.*;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Size;
import javax.validation.groups.Default;
import java.util.List;

/**
 * @author wubc683
 * @desc
 * @date 2021-08-25 17:41
 */
@RestController
@Api(tags = "角色接口")
@Validated
public class RoleController {
    @Autowired
    ResponseUtils responseUtils;
    @Autowired
    RoleService RoleService;
    @Autowired
    UserService userService;

    @PostMapping("/role")
    @ApiOperation(value = "增加角色")
    @ApiOperationSupport(order = 10, includeParameters = {"role.name", "role.description"})
    public ResponseUtils addRole(@Validated({RoleNameGroup.class, RoleDescriptionGroup.class}) @RequestBody Role role) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        RoleService.addRole(implUser, role);
        return responseUtils.buildSuccessResponse();
    }

    @DeleteMapping("/role/{id}")
    @ApiOperation("删除角色")
    @ApiOperationSupport(order = 20)
    public ResponseUtils deleteRole(@PathVariable("id") @ApiParam(value = "角色id",required = true) Long id) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        RoleService.deleteRole(implUser, id);
        return responseUtils.buildSuccessResponse();
    }

    @PutMapping("/role")
    @ApiOperation("修改角色")
    @ApiOperationSupport(order = 30)
    public ResponseUtils updateRole(@Validated({IdGroup.class,RoleNameGroup.class,RoleDescriptionGroup.class}) @RequestBody Role role) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        RoleService.updateRole(implUser, role);
        return responseUtils.buildSuccessResponse();
    }

    @GetMapping("/role/{id}")
    @ApiOperation("查询角色")
    @ApiOperationSupport(order = 40)
    public ResponseUtils<Role> getRole(@PathVariable("id") @ApiParam(value = "角色id",required = true) Long id) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        User userInfoById = userService.getUserInfoById(implUser.getId());
        Role role = RoleService.getRole(userInfoById, id);
        return responseUtils.buildSuccessResponse(role);
    }

    @GetMapping("/roles")
    @ApiOperation(value = "获取用户所有角色")
    @ApiOperationSupport(order = 50)
    public ResponseUtils<List<Role>> getRoles() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        List<Role> roles = RoleService.getRoles(implUser);
        return responseUtils.buildSuccessResponse(roles);
    }
//
//    @PostMapping("/menu/{rid}")
//    @ApiOperation(value = "为用户角色创建操作菜单")
//    @ApiOperationSupport(order = 60,includeParameters = "menuGroups.id,menuGroups.selected")
//    public ResponseUtils saveRoleMenu(@PathVariable("rid") @ApiParam(value = "角色id",required = true) Long rid,
//                                      @Valid @ListNotHasNull @Size(min=1) @RequestBody List<MenuGroup> menuGroups) {
//        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
//        User implUser = userDetailsImpl.getUser();
//        User userInfoById = userService.getUserInfoById(implUser.getId());
//        RoleService.saveRoleMenu(userInfoById, rid, menuGroups);
//        return responseUtils.buildSuccessResponse();
//    }

    @PutMapping("/menu/{rid}")
    @ApiOperation(value = "修改角色菜单")
    @ApiOperationSupport(order = 70,includeParameters = {"rid","menuGroups.id","menuGroups.selected"})
    public ResponseUtils updateRoleMenu(@PathVariable("rid") @ApiParam(value = "角色id",required = true) Long rid,
                                        @Valid @ListNotHasNull @Size(min=1) @RequestBody List<MenuGroup> menuGroups) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        User userInfoById = userService.getUserInfoById(implUser.getId());
        RoleService.updateRoleMenu(userInfoById, rid, menuGroups);
        return responseUtils.buildSuccessResponse();
    }

    @GetMapping("/menu/{rid}")
    @ApiOperation(value = "获取角色对应的菜单组")
    @ApiOperationSupport(order = 80)
    public ResponseUtils<List<MenuGroup>> getRoleMenu(@PathVariable("rid") @ApiParam(value = "角色id",required = true) Long rid) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        User userInfoById = userService.getUserInfoById(implUser.getId());
        List<MenuGroup> roleMenuGroup = RoleService.getRoleMenuGroup(userInfoById, rid);
        return responseUtils.buildSuccessResponse(roleMenuGroup);
    }

//    @GetMapping("/menus")
//    @ApiOperation(value = "获取所有可分配菜单", notes = "获取所有可分配给关联用户的功能菜单")
//    @ApiOperationSupport(order = 90)
//    public ResponseUtils<List<Menu>> getAllRoleMenu() {
//        List<Menu> RoleMenu = RoleService.getAllRoleMenu();
//        return responseUtils.buildSuccessResponse(RoleMenu);
//    }

}
